<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <!--分页插件-->
    <script src="/after/js/laypage/laypage.js" charset="utf-8"></script>
    <script src="/after/js/layer/layer.js" charset="utf-8"></script>
    <link href="/after/js/laypage/skin/laypage.css" rel="stylesheet">


    <link rel="stylesheet" href="/after/css/bootstrap.min.css">
    <link rel="stylesheet" href="/after/css/laypage.css">
    <script type="text/javascript" src="/after/js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="/after/js/laypage/laypage.js"></script>
    <script type="text/javascript" src="/after/js/layer/layer.js"></script>
    <script type="text/javascript" src="/after/js/vue/vue.min.js"></script>
    <script src="/after/jquery.validation/1.14.0/jquery.validate.min.js"></script>
    <script src="/after/jquery.validation/1.14.0/messages_zh.min.js"></script>


    <script src="/after/laydate/laydate.js"></script>
    <script src="/after/css/laydate.css"></script>
    <style>
        input.error {
            border: 1px solid #E6594E;
        }
    </style>
</head>
<body>
<div class="container">
    <form id="addUserForm">
        <br/>
        <div class="form-group">
            <label for="name">名称：</label>
            <input type="text" class="form-control" id="name" name="name" placeholder="输入名称">
        </div>
        <div class="form-group">
            <label for="money">金额：</label>
            <input type="text" class="form-control" id="money" name="money" placeholder="输入金额">
        </div>
        <div class="form-group">
            <label for="conditio">满减条件：</label>
            <input type="text" class="form-control" id="conditio" name="conditio" placeholder="输入满减条件">
        </div>
        <div class="form-group">
            <label for="startTime">开始时间：</label>
            <input type="text" class="form-control" id="startTime" name="startTime" placeholder="输入开始时间">
        </div>
        <div class="form-group">
            <label for="endTime">结束时间：</label>
            <input type="text" class="form-control" id="endTime" name="endTime" placeholder="输入结束时间">
        </div>
        <div class="form-group">
            <label for="coun">总数：</label>
            <input type="text" class="form-control" id="coun" name="coun" placeholder="输入总数">
        </div>
        <div class="form-group">
            <button type="button" id="saveBtn" class="btn btn-success">提交</button>
            <button type="button" id="cancelBtn" class="btn btn-default">取消</button>
        </div>
    </form>
</div>

<script>
   //日期控件
    lay('#version').html('-v' + laydate.v);

    //执行一个laydate实例
    laydate.render({
        elem: '#startTime'
        , type: 'datetime'
    });

   lay('#version').html('-v' + laydate.v);

   //执行一个laydate实例
   laydate.render({
       elem: '#endTime'
       , type: 'datetime'
   });


    var addUser = function () {
        //非空校验
        if (!check().form()) {
            return;
        }
        //检验时间大小
        var createTime=($("#startTime").val());
        var endTime=($("#endTime").val());
        var d1 = new Date(createTime.replace(/\-/g, "\/"));
        var d2 = new Date(endTime.replace(/\-/g, "\/"));
        if(d2-d1<=0) {
            layer.msg("开始时间不能大于或等于结束时间");
            return false;
        }
        //检验金额
        var money= parseInt(document.getElementById("money").value);
        var conditio = parseInt (document.getElementById("conditio").value);
        if(money>conditio){
            layer.msg("满减金额有误！");
            return;
        }

        $.ajax({
            url: "/czgDiscount/add.do",
            type: "POST",
            dataType: "json",
            data: {
                "name": $("#name").val(),
                "money": $("#money").val(),
                "conditio": $("#conditio").val(),
//                "sTime": $("#startTime").val(),
//                "eTime": $("#endTime").val(),
                "startTime": new Date($("#startTime").val()),
                "endTime":new Date($("#endTime").val()) ,
                "coun": $("#coun").val(),
                "remain": $("#coun").val()
            },
            success: function (msg) {
                $('#cancelBtn').click();
            },error:function(){
                layer.msg("信息有误！请检查");
            }
        });
    }

    $('#saveBtn').on('click', function () {
        addUser();
    });

    $('#cancelBtn').on('click', function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.getUserPageList();
        parent.layer.close(index);
    });


    //校验字段是否正确
    function check() {
        /*返回一个validate对象，这个对象有一个form方法，返回的是是否通过验证*/
        return $("#addUserForm").validate({
            rules: {
                name: {
                    required: true
                },
                money: {
                    required: true
                },
                conditio: {
                    required: true
                },
                startTime: {
                    required: true
                },
                endTime: {
                    required: true
                },
                coun: {
                    required: true
                }
            },
            messages: {
                name: {
                    required: ""
                },
                money: {
                    required: ""
                },
                conditio: {
                    required: ""
                },
                startTime: {
                    required: ""
                },
                endTime: {
                    required: ""
                },
                coun: {
                    required: ""
                }
            }
        });
    }

</script>
</body>
</html>